package pers.cj.pdos.realm;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.Permission;
import pers.cj.pdos.entity.User;
import pers.cj.pdos.service.*;
import pers.cj.pdos.vo.UserVo;

import java.util.ArrayList;
import java.util.List;

public class CustomRealm extends AuthorizingRealm {
    @Autowired
    IPermissionService permissionService;
    @Autowired
    IMenu1Service menu1Service;
    @Autowired
    IIndustryService industryService;
    @Autowired
    ICompanyService companyService;
    @Autowired
    IUserService userService;
    @Autowired
    IBusinessSetService businessSetService;
    @Autowired
    IStoreService storeService;
    @Autowired
    IHouseService houseService;
    @Autowired
    IRoleService roleService;
    @Autowired
    IRetailSetService retailSetService;
    @Autowired
    ISettAccountService settAccountService;
    @Autowired
    IAttrNameService attrNameService;
    @Autowired
    IAttrService attrService;
    @Autowired
    ITypeService typeService;
    @Autowired
    IGoodsService goodsService;
    @Autowired
    IGoodsUnitService goodsUnitService;
    @Autowired
    IUnitService unitService;

    @Override
    public String getName() {
        return "CustomRealm";
    }

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权--------------------------------------------");
        //获取身份信息
        User user = (User) principalCollection.getPrimaryPrincipal();
        List<Permission> list = permissionService.findByUserId(user.getId());

        List<String> permissionList = new ArrayList<>();
        for (Permission permission : list) {
            permissionList.add(permission.getCode());
        }

        //构建shiro授权信息
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addStringPermissions(permissionList);
        return info;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户名
        String username = (String) token.getPrincipal();
        UserVo userVo = userService.getVoByUserName(username);

        if (userVo == null) {
            return null;
        }

        if (userVo.getStatus() != 1){
            //状态时未启用
            return null;
        }
        String password = userVo.getPassword();
        String sal = userVo.getSalt();

        userVo.setPassword("");
        userVo.setSalt("");

        Company company = companyService.getById(userVo.getCompanyId());
        userVo.setAdmin(company.getAccount().equals(userVo.getUsername()));//用户名=公司账号 表示是管理员账号

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userVo,password, ByteSource.Util.bytes(sal),this.getName());

        //一些耗时的数据库操作可以先在此函数缓存
        prepareTime(userVo);
        return info;
    }

    //清空缓存
    public void clearCached() {
        //清空所有用户的身份缓存信息
        PrincipalCollection principalCollection = SecurityUtils.getSubject().getPrincipals();
        super.clearCache(principalCollection);
    }

    /**
     * 登录时一些耗时的数据库操作可以提前在此函数执行
     * @param user
     */
    public void prepareTime(User user) {
        //获取公司信息
        Company company = companyService.getById(user.getCompanyId());

        //登录用户信息
        userService.getById(user.getId());
        //权限
        permissionService.findByUserId(user.getId());
        permissionService.listAll();
        //获取可能会启用sn的行业
        industryService.getByName("家电数码");
        //登录公司所属行业
        industryService.getById(company.getIndustryId());
        //行业
        industryService.listAll();
    }
}
